package com.baidu.lister;

import com.baidu.utils.SendSms;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author ZYH
 * @version 1.0
 * @description:TODO
 * @date 2022/1/6 19:27
 */
@Service
public class DemoLister {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @RabbitListener(queues = "demoyk1223sendqueue")
    public void sendLister(String phone, Message message, Channel channel) throws IOException {
        String smcode="";
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            int i1 = random.nextInt(9);
            smcode+=i1;
        }
        System.out.println(smcode);
        stringRedisTemplate.opsForValue().set(phone,smcode+"",1, TimeUnit.MINUTES);
        SendSms sendSms = new SendSms();
        sendSms.sendLoginSms(phone,smcode+"");
        long tag = message.getMessageProperties().getDeliveryTag();
        try {
            System.out.println("发送成功");
            channel.basicAck(tag,false);
        }catch(Exception e){
            channel.basicNack(tag,false,false);
        }
        System.out.println("给"+phone+"发送信息成功");
    }
}
